package com.js.mail.ware.service.impl;

import com.js.common.constant.WareConst;
import com.js.mail.ware.entity.PurchaseDetailEntity;
import com.js.mail.ware.service.PurchaseDetailService;
import com.js.mail.ware.service.WareSkuService;
import com.js.mail.ware.vo.MergeVo;
import com.js.mail.ware.vo.PurchaseDoneVo;
import com.js.mail.ware.vo.PurchaseItemDoneVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.js.common.utils.PageUtils;
import com.js.common.utils.Query;

import com.js.mail.ware.dao.PurchaseDao;
import com.js.mail.ware.entity.PurchaseEntity;
import com.js.mail.ware.service.PurchaseService;
import org.springframework.transaction.annotation.Transactional;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {

    @Autowired
    PurchaseDetailService purchaseDetailService;


    @Autowired
    WareSkuService wareSkuService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageUnreceive(Map<String, Object> params) {
        QueryWrapper<PurchaseEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1).or().eq("status", 0);

        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void merge(MergeVo mergeVo) {
        Long purchaseId = mergeVo.getPurchaseId();
        if (purchaseId == null) {
            PurchaseEntity purchaseEntity = new PurchaseEntity();
            purchaseEntity.setCreateTime(new Date());
            purchaseEntity.setUpdateTime(new Date());
            purchaseEntity.setStatus(WareConst.PurchaseStatusEnum.CREATED.getCode());
            this.save(purchaseEntity);

            purchaseId = purchaseEntity.getId();
        }
        List<Long> items = mergeVo.getItems();

        Long finalPurchaseId = purchaseId;
        List<PurchaseDetailEntity> collect = items.stream().map(it -> {
            PurchaseDetailEntity entity = new PurchaseDetailEntity();
            entity.setId(it);
            entity.setPurchaseId(finalPurchaseId);
            return entity;
        }).collect(Collectors.toList());

        purchaseDetailService.updateBatchById(collect);

        PurchaseEntity entity = new PurchaseEntity();
        entity.setUpdateTime(new Date());
        entity.setId(purchaseId);
        this.updateById(entity);
    }

    @Override
    public void received(List<Long> ids) {
        //确认采购单是新建或者已经分配的状态
        List<PurchaseEntity> collect = ids.stream().map(it -> {
            PurchaseEntity byId = this.getById(it);
            return byId;
        }).filter(item -> {
            if (item.getStatus() == WareConst.PurchaseStatusEnum.CREATED.getCode() ||
                    item.getStatus() == WareConst.PurchaseStatusEnum.ASSIGNED.getCode()) {
                return true;
            } else
                return false;
        }).map(it -> {
            it.setStatus(WareConst.PurchaseStatusEnum.RECEIVE.getCode());
            it.setUpdateTime(new Date());
            return it;
        }).collect(Collectors.toList());

        //改变采购但的状态
        this.updateBatchById(collect);

        //改变采购项的状态
        for (PurchaseEntity entity : collect) {
            List<PurchaseDetailEntity> purchaseDetailEntities = purchaseDetailService.listDetailByPurchaseId(entity.getId());
            List<PurchaseDetailEntity> collect1 = purchaseDetailEntities.stream().map(it -> {
                PurchaseDetailEntity entity1 = new PurchaseDetailEntity();
                entity1.setId(it.getId());
                entity1.setStatus(WareConst.PurchaseDetailStatusEnum.BUYING.getCode());
                return entity1;
            }).collect(Collectors.toList());
            purchaseDetailService.updateBatchById(collect1);
        }
    }

    @Transactional
    @Override
    public void done(PurchaseDoneVo doneVo) {

        Long id = doneVo.getId();

        //2、改变采购项的状态
        Boolean flag = true;
        List<PurchaseItemDoneVo> items = doneVo.getItems();

        List<PurchaseDetailEntity> updates = new ArrayList<>();
        for (PurchaseItemDoneVo item : items) {
            PurchaseDetailEntity detailEntity = new PurchaseDetailEntity();
            if(item.getStatus() == WareConst.PurchaseDetailStatusEnum.HASERROR.getCode()){
                flag = false;
                detailEntity.setStatus(item.getStatus());
            }else{
                detailEntity.setStatus(WareConst.PurchaseDetailStatusEnum.FINISH.getCode());
                ////3、将成功采购的进行入库
                PurchaseDetailEntity entity = purchaseDetailService.getById(item.getItemId());
                wareSkuService.addStock(entity.getSkuId(),entity.getWareId(),entity.getSkuNum());

            }
            detailEntity.setId(item.getItemId());
            updates.add(detailEntity);
        }
        purchaseDetailService.updateBatchById(updates);

        //1、改变采购单状态
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(id);
        purchaseEntity.setStatus(flag?WareConst.PurchaseStatusEnum.FINISH.getCode():WareConst.PurchaseStatusEnum.HASERROR.getCode());
        purchaseEntity.setUpdateTime(new Date());
        this.updateById(purchaseEntity);
    }

}